package com.feicheng.iou.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.entity.ReservationInterview;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.mapper.ReservationInterviewMapper;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.ReservationInterviewService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
@Setter
public class ReservationInterviewServiceImpl extends ServiceImpl<ReservationInterviewMapper, ReservationInterview> implements ReservationInterviewService {

    @Autowired
    private ReservationInterviewMapper reservationInterviewMapper;  //预约到访mapper


//    /**
//     * @Author wuweinan
//     * @Description 预约到访时间设置
//     * @Date 2021-6-29 15:56
//     * @param reservationInterview
//     * @return void
//     */
//    @Override
//    public ResultInfo insertInterviewReservationTime(ReservationInterview reservationInterview) {
//
//        log.info("ReservationInterviewServiceImpl的insertInterviewReservationTime方法进入");
//        reservationInterview.setCt(LocalDateTime.now());
//        //添加一条预约接待信息
//        Integer insertCount = reservationInterviewMapper.insert(reservationInterview);
//
//        //校验插入条数是否正确
//        if(insertCount != 1){
//            throw new SystemException(ReturnCode.COMMON_RESERVATION_INTERVIEW_WRONG_INSERT_COUNT);
//        }
//
//        log.info("ReservationInterviewServiceImpl的insertInterviewReservationTime方法返回");
//        return ResultInfo.success();
//    }

    /**
     * @Author wuweinan
     * @Description 更新预约到访时间设置
     * @Date 2021-6-29 15:46
     * @param reservationInterview
     * @return ResultInfo
     */
    @Override
    public ResultInfo updateInterviewReservationTime(ReservationInterview reservationInterview) {
        //根据学生id修改预约回访的datetime字段,因为student和reservationInterview表示一对一的了
        UpdateWrapper<ReservationInterview> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("datetime", reservationInterview.getDatetime() ).
                set("uu", reservationInterview.getUu()).
                set("autor", reservationInterview.getAutor()).
                eq("id", reservationInterview.getId()).
                eq("student_id", reservationInterview.getStudentId());

        int update = reservationInterviewMapper.update(null, updateWrapper);

        if(update != 1){
            throw new SystemException(ReturnCode.UPDATE_RESERVATION_INTERVIEW_FAIL);
        }
        return ResultInfo.success();
    }
}
